package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Process;
import com.mrd.gtimp.base.service.ProcessBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author hejj
 */

@Controller
@RequestMapping("/base")
public class ProcessAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private ProcessBiz processBiz;

    private String bizType;

    private List<Process> processes;

    /**
     * public String show() { try { page = initPage(ProcessBiz.findEntityCount(filters)); Order
     * order = new Order(); order.add(sidx, sord); Processs = ProcessBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order); }
     * catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
     **/

    @RequestMapping("/process-main")
    public String main(Model model) {
//        title = "业务流程管理";
//        left = "process-tree.do";
//        // right = "mod-pms.do";
//        right = "";
        model.addAttribute("title", "业务流程管理");
        model.addAttribute("left", "process-tree");
        model.addAttribute("right", "");
        return WebPathConstants.FRAME_WEB_PATH;
    }

    @RequestMapping("/process-find")
    public String find() {
        return "gtimp/base/process/process-list";
    }

    @RequestMapping("/process-tree")
    public String tree() {
        return "/gtimp/base/process/process-tree";
    }

    @RequestMapping("/process-manage")
    public String manage(Process process) {
        if (null != process) {
            if (StringUtils.isNotEmpty(process.getProcessId())) {
                try {
                    process = processBiz.findEntityById(process.getProcessId());
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return "/gtimp/base/process/process-manage";
    }

    @RequestMapping("/process-getData")
    @ResponseBody
    public Process getData(Process process) {
        if (null != process) {
            if (StringUtils.isNotEmpty(process.getProcessId())) {
                try {
                    process = processBiz.findEntityById(process.getProcessId());
                    return process;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @RequestMapping("/process-listProcess")
    @ResponseBody
    public Page<Process> listProcess(Process process, Jqgrid jqgrid) {
        try {
            if (process != null && StringUtils.isNotEmpty(process.getBizType())) {
                SearchRule searchRule = new SearchRule("bizType", SearchRuleOption.eq, bizType);
                jqgrid.addRule(searchRule);
            }
            Page<Process> processes = processBiz.findByJqgrid(jqgrid);
            return processes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/process-saveOrUpdateProcess")
    @ResponseBody
    public Result saveOrUpdateProcess(Process process) {
        Result msg;
        try {
            if (null != process) {
                if (StringUtils.isEmpty(process.getProcessId())) {
                    process.setProcessId(null);
                } else {

                }
                //// TODO: 2016/8/22 processState  保存对象是不能保存processState属性
                processBiz.saveOrupdateEntity(process);
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    @Resource
    public void setProcessBiz(ProcessBiz processBiz) {
        this.processBiz = processBiz;
    }

    public String getBizType() {
        return bizType;
    }

    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    public List<Process> getProcesses() {
        return processes;
    }

    public void setProcesses(List<Process> processes) {
        this.processes = processes;
    }

}
